
/**
 * 
 * Name：观察者
 * Author：fanzhongdeqiuchun(362460803@qq.com）
 * Desc:观察者,把目标类和它的监听函数绑定到一个观察者身上。
 * 
*/

import Notification from "./Notification"
import Notifier from "./Notifier";

type func = (notification: Notification) => void;
export default class Observer {
    private notify: func = null as any;
    private context: Notifier = null as any;

    constructor(notifyMethod: func, notifyContext: Notifier) {
        if (typeof notifyMethod != "function") {
            console.log("mvp error: notifyMethod is not function.");
            return;
        }
        this.setNotifyMethod(notifyMethod);

        if (typeof notifyContext != "object") {
            console.log("mvp error: notifyContext is not object.");
            return;
        }
        this.setNotifyContext(notifyContext);

    }

    public getNotifyMethod(): func {
        return this.notify;
    };
    public setNotifyMethod(notifyMethod: func): void {
        this.notify = notifyMethod;
    };
    public getNotifyContext(): Notifier {
        return this.context;
    };
    public setNotifyContext(notifyContext: Notifier): void {
        this.context = notifyContext;
    };
    public notifyObserver(notification: Notification) {
        this.getNotifyMethod().call(this.getNotifyContext(), notification);
    };
    public compareNotifyContext(ob: Notifier | string) {
        let result = false;
        if (ob instanceof Notifier) {
            result = ob === this.context
        }
        else if (typeof ob === "string") {
            result = ob === this.context.getName()
        }
        return result;
    };



}




